/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ec;

import java.io.PrintStream;

/**
 *
 * @author alex
 */
public class asmc64 extends ProduceAssembly {

    asmc64(PrintStream out)
    {
        super(out);
    }

    @Override
    public void LoadConst(String str) {
        //super.LoadConst(str);
        int low, high;
        low=Integer.parseInt(str);
        low=low & 0xFFFF;
        high=(low>>8);
        low=low & 0xFF;
        if (low!=high) {
            Emitln("lda\t#<"+str);
            Emitln("ldx\t#>"+str);
        } else {
            Emitln("lda\t#<"+str);
            Emitln("tax");
        }

    }

    @Override
    public void Store(String str) {
        //super.Store(str);
        Emitln("sta\t"+str);
        Emitln("stx\t"+str+"+1");
    }

    @Override
    public void LoadVar(String str) {
        Emitln("lda\t"+str);
        Emitln("ldx\t"+str+"+1");
    }

    @Override
    public void PushPrimary() {
        Emitln("pha");
        Emitln("txa");
        Emitln("pha");
    }

    @Override
    public void BranchFalse(String label) {
        String skiplabel=NewLabel();
        Emitln("bne\t"+skiplabel);
        Emitln("jmp\t"+label);
        EmitNoTabln(skiplabel);
    }

    @Override
    public void SetNEqual() {
        String skip1=NewLabel();
        String skip2=NewLabel();
        Emitln("beq\t"+skip1);
        Emitln("lda\t#1");
        Emitln("ldx\t#0");
        Emitln("beq\t"+skip2);
        EmitNoTabln(skip1);
        Emitln("lda\t#0");
        Emitln("tax");
        EmitNoTabln(skip2);
    }

    @Override
    public void SetEqual() {
        String skip1=NewLabel();
        String skip2=NewLabel();
        Emitln("bne\t"+skip1);
        Emitln("lda\t#1");
        Emitln("ldx\t#0");
        Emitln("beq\t"+skip2);
        EmitNoTabln(skip1);
        Emitln("lda\t#0");
        Emitln("tax");
        EmitNoTabln(skip2);
    }

    @Override
   public void PrintPrimary() {
       Emitln("sta\t$fa");
       Emitln("txa");
       Emitln("ldx\t$fa");
       Emitln("jsr\t$bdcd");
   }


    @Override
   public void Allocate(String Name, String Val)
   {
       EmitNoTabln(Name + ":\t.word\t" + Val);
   }


    @Override
    public void PopAdd()
    {
        Emitln("sta\t$fa");
        Emitln("stx\t$fb");
        Emitln("pla");
        Emitln("tax");
        Emitln("pla");
        Emitln("clc");
        Emitln("adc\t$fa");
        Emitln("sta\t$fa");
        Emitln("txa");
        Emitln("adc\t$fb");
        Emitln("sta\t$fb");
        Emitln("lda\t$fa");
        Emitln("ldx\t$fb");
    }
}
